var that,
  api = new (require('../../utils/api.js')),
  app = getApp(),
  util = new (require('../../utils/util.js'));
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    site: '请您要充值的站点',
    sitearr: [],
    selindex: -1,
    siteid: -1,
    isFocus: !1,//控制input 聚焦
    issetpay: !1,//是否输入支付密码
    ismodel: !1,
    code: 0,
    logininfo: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    that.getLocation()
  },
  getLocation() {
    let data = {}
    wx.getLocation({
      success: res => {
        console.log(res, 'rwesres');
        wx.showLoading({
          title: '加载中',
        })
        // api.systemstation({lng:res.longitude,lat:res.latitude},res1=>{
        //   if(res1.data.code==0){
        //     that.setData({sitearr:res1.data.entity.data})
        //   }
        // })
        data = { lng: res.longitude, lat: res.latitude }
        that.systemstation(data)
        that.getdate()
      },
      fail: res => {
        data = { lng: '', lat: '' }
        that.systemstation(data)
        that.getdate()
        wx.getSetting({
          success: res => {
            if (typeof (res.authSetting['scope.userLocation']) != 'undefined' && !res.authSetting['scope.userLocation']) {

              wx.showModal({
                content: '加油站需要获取地址位置权限，是否前往开启',
                confirmText: '前往开启',
                success: res => {
                  if (res.confirm) {
                    wx.openSetting({
                      success: res => {
                        if (res.authSetting['scope.userLocation']) {
                          // 授权成功，重新定位
                          wx.getLocation({
                            success: res => {
                              that.setData({ lat: res.latitude, lng: res.longitude })
                              data = { lng: res.longitude, lat: res.latitude }
                              that.systemstation(data)
                            }
                          });
                        } else {
                          // 没有允许定位权限
                          data = { lng: '', lat: '' }
                          that.systemstation(data)
                        }
                      }
                    });
                  } else {
                    data = { lng: '', lat: '' }
                    that.systemstation(data)
                  }
                }
              })
            } else {
              wx.getLocation({
                success: res => {
                  that.setData({ lat: res.latitude, lng: res.longitude })
                  data = { lat: res.latitude, lng: res.longitude }
                  that.systemstation(data)
                }
              })
            }
          }
        })
      }
    })
  },
  systemstation(data) {
    let siteid = -1, site = '请您要充值的站点'
    api.systemstation(data, res1 => {
      if (res1.data.code == 0) {
        if (res1.data.entity.data.length == 1) {
          siteid = res1.data.entity.data[0].id
          site = res1.data.entity.data[0].name
        } else {
          res1.data.entity.data.forEach(x => {
            if (parseInt(x.distance) < 80) {
              siteid = x.id
              site = x.name
            }
          })
        }
        // that.setData({site:sitearr[e.detail.value].name,siteid:sitearr[e.detail.value].id})

        that.setData({ sitearr: res1.data.entity.data, site, siteid })
      }
    })
  },
  getdate() {
    let { list, balance } = that.data, newbalance = wx.getStorageSync('amount');
    api.getrechargelist(res => {
      wx.hideLoading()
      if (res.data.code == 0) {
        console.log(res.data.entity.data, 'res.data.entity.data');

        res.data.entity.data.forEach(x => {
          x.save = x.chargeFee - x.payFee
          if (x.attr == 2) {
            x.iscustomerlike = 0
            x.isshoplike = 1
            x.chargeFee = parseInt(x.chargeFee)
            x.payFee = parseInt(x.payFee)
          } else if (x.attr == 1) {
            x.isshoplike = 0
            x.iscustomerlike = 1
            x.chargeFee = parseInt(x.chargeFee)
            x.payFee = parseInt(x.payFee)
          } else {
            x.isshoplike = 1
            x.iscustomerlike = 1
          }
          list.push(x)
        })
      }
      let newbalancearr = Number(newbalance).toFixed(2).split('.')
      that.setData({ list, newbalance, newbalancearr })
    })
  },
  selrecharge(e) {
    console.log(e.currentTarget.dataset.index, 'eeee');
    that.setData({ selindex: e.currentTarget.dataset.index })
  },
  pickchange(e) {
    let { sitearr } = that.data
    that.setData({ site: sitearr[e.detail.value].name, siteid: sitearr[e.detail.value].id })
  },
  // 充值
  clickrecharge() {
    let { selindex, siteid } = that.data, err = ''
    if (siteid == -1) err = '请选择要充值的站点'
    if (selindex == -1) err = '请选择要充值的金额'
    if (err.length > 0) {
      wx.showModal({
        title: '提示',
        content: err,
        showCancel: !1,
      })
      return;
    }
    wx.showLoading({
      title: "加载中",
    })
    api.systemcorpinfo(res => {
      wx.hideLoading()
      if (res.data.code == 0) {
        if (res.data.entity.data.corpExpired == 1) {
          wx.showModal({
            title: '提示',
            content: '系统已过期，无法充值',
            showCancel: !1,
          })
          return;
        } else {
          wx.navigateTo({
            url: '../payment/payment?id=' + siteid,
          })
        }
      } else {
        wx.showToast({
          title: res.data.message,
          icon: 'none'
        })
      }
    })

  },
  rechargeRecord() {
    wx.navigateTo({
      url: '../rechargeRecord/rechargeRecord',
    })
  },
  set_Focus() {//聚焦input
    console.log('isFocus', this.data.isFocus)
    this.setData({
      isFocus: !0
    })
  },
  set_wallets_password(e) {//获取钱包密码
    let { issetpay } = that.data
    if (issetpay) {
      this.setData({
        wallets_password: e.detail.value, reconfirmpwd: e.detail.value
      });
    } else {
      this.setData({
        wallets_password: e.detail.value, pwd: e.detail.value
      });
    }

  },
  setpwd() {
    let { pwd } = that.data
    if (pwd.length != 6) {
      wx.showToast({
        title: '请输入完整的支付密码',
        icon: 'none'
      })
      return;
    }
    setTimeout(() => { //延遲發送，防止input沒有清空
      that.setData({
        wallets_password: '', reconfirmpwd: '', issetpay: !0,
      }, () => {
        console.log(that.data.wallets_password.length, 'wallets_passwordwallets_password.length');
        console.log(that.data.wallets_password, 'wallets_passwordwallets_password');
        console.log(that.data.reconfirmpwd, 'reconfirmpwdreconfirmpwd');

      })
    }, 100)
    //  that.setData({wallets_password:'',reconfirmpwd:'',issetpay:!0,},()=>{
    //   console.log(that.data.wallets_password.length,'wallets_passwordwallets_password.length');
    //   console.log(that.data.wallets_password,'wallets_passwordwallets_password');
    //   console.log(that.data.reconfirmpwd,'reconfirmpwdreconfirmpwd');

    //  })
  },
  //  再次确认密码
  reconfirm() {
    let { pwd, reconfirmpwd } = that.data, err = ''
    if (reconfirmpwd.length != 6) err = '请输入完整的支付密码'
    if (reconfirmpwd != pwd) err = '密码输入错误'
    if (err != '') {
      wx.showToast({
        title: err,
        icon: 'none'
      })
      return;
    }
    api.usersetpwd(pwd, res => {
      if (res.data.code == 0) {
        wx.showToast({
          title: '设置密码成功',
          icon: 'none',
          success() {
            that.setData({ issetpay: !1, ismodel: !1, wallets_password: '', pwd: '', reconfirmpwd: '' }, () => {
              wx.navigateTo({
                url: '../payment/payment',
              })
            })
          }
        })
      } else {
        wx.showToast({
          title: res.data.message,
          icon: 'none'
        })
      }
    })
  },
  delmodel() {
    that.setData({ ismodel: !1, issetpay: !1, wallets_password: '', pwd: '', reconfirmpwd: '' })
  },
  // 授权手机号
  getPhoneNumber(e) {
    let { selindex, siteid } = that.data
    let { userInfo } = that.data
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      api.wxphone({ encryptedData: e.detail.encryptedData, iv: e.detail.iv }, res => {
        if (res.data.code == 0) {
          let { phoneNumber } = res.data.entity.data
          userInfo.phone = phoneNumber
          that.setData({ userInfo })
          wx.setStorageSync('userInfo', userInfo)
          if (siteid != -1 && selindex != -1) {
            wx.navigateTo({
              url: '../payment/payment?id=' + siteid,
            })
          }
        }
      })
    } else {
      wx.showModal({
        content: '授权手机号之后才能充值，请授权',
        showCancel: !1,
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let userInfo = wx.getStorageSync('userInfo')
    // userInfo.phone=''
    that.setData({ userInfo })
    console.log(that.data.code, 'that.data.codethat.data.codethat.data.code');
    let { code } = that.data
    if (code == 20637) {
      that.setData({ ismodel: !0, code: 1, wallets_password: '', pwd: '', reconfirmpwd: '' })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    that.setData({ code: 1 })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    that.setData({ code: 1 })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})